package www.lagou.app;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;
import java.util.List;

public class TestEmployee {
    public static void main(String[] args) {
        //1.获取自动模式QueryRunner()
        QueryRunner queryRunner = new QueryRunner(DruidUtils.dataSource);

        //2.编写sql
        //需求1: 查询所有的员工信息 (不包含没有部门的员工)。
        String sql = "SELECT * FROM employee WHERE did IS NOT NULL;";
        //需求2: 查询每个员工的 姓名, 薪资 和 所属部门名称
        String sql2 = "SELECT e.name,e.`salary`,d.`deptname` FROM employee e LEFT JOIN dept d ON e.`did` = d.`id` ";
        //3.执行sql
        try {
            List<Object[]> query = queryRunner.query(sql, new ArrayListHandler());

            //打印返回的结果
            for (Object[] objects : query) {
                for (Object object : objects) {
                    System.out.print("\t" + object.toString());
                }
                System.out.println();
            }

            System.out.println("-------------------");
            List<Object[]> query2 = queryRunner.query(sql2, new ArrayListHandler());
            //打印返回的结果
            for (Object[] objects : query2) {
                for (Object object : objects) {
                    if(object == null){
                        System.out.println("\t");
                    }else {
                        System.out.print("\t" + object.toString());
                    }
                }
                System.out.println();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
